﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ColumnSorter.cs" company="SMART business">
//   Copyright (c) 2014 All Rights Reserved
// </copyright>
// <summary>
//   The interface.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Collections;
using System.Windows.Forms;

namespace UsersSettingsTool.Additional_Classes.Classes
{
    public class ColumnSorter : IComparer
    {
         private readonly int _column;

        /// <summary>
        /// The order.
        /// </summary>
        private readonly SortOrder _order;

        public ColumnSorter(int column, SortOrder order)
        {
            _column = column;
            _order = order;
        }

        public int Compare(object x, object y)
        {
            if (_order == SortOrder.Descending)
            {
                return 1;
            }

            if (((ListViewItem)x).SubItems.Count <= _column || ((ListViewItem)y).SubItems.Count <= _column)
            {
                return 1;
            }

            return string.CompareOrdinal(((ListViewItem)x).SubItems[_column].Text, ((ListViewItem)y).SubItems[_column].Text);
        }
    }
}